package com.zyzd.baseokhttp.base.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.zyzd.baseokhttp.R;
import com.zyzd.baseokhttp.base.adapter.BaseRecyclerAdapter;
import com.zyzd.baseokhttp.widget.recycler.RecyclerRefreshLayout;
import com.zyzd.commontools.log.TLog;

import java.util.ArrayList;
import java.util.Date;

import butterknife.Bind;


public abstract class BaseRecyclerViewActivity<T> extends BaseNetActivity<ArrayList<T>> implements BaseRecyclerAdapter.OnItemClickListener, RecyclerRefreshLayout.SuperRefreshLayoutListener {
    protected static int initPageIndex = 0;
    @Bind(R.id.recyclerView)
    protected RecyclerView mRecyclerView;
    @Bind(R.id.refreshLayout)
    protected RecyclerRefreshLayout mRefreshLayout;
    @Bind(R.id.iv_bg)
    protected ImageView mIvBg;
    @Bind(R.id.toolbar)
    protected Toolbar mToolbar;
    @Bind(R.id.toolbar_title)
    protected TextView mToolbarTitle;
    protected BaseRecyclerAdapter<T> mAdapter;
    protected boolean mIsRefresh;
    protected long stampTime;
    protected int nextPageIndex;
    protected boolean hasMore;
    protected ArrayList<T> mList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_recycler_view;
    }

    @Override
    public void initData() {

        setToolBar();

        mRecyclerView.setLayoutManager(getLayoutManager());//设置布局管理器，默认垂直线性
        mAdapter = getRecyclerAdapter();//获取adapter
        mAdapter.setState(BaseRecyclerAdapter.STATE_HIDE, false);
        mRecyclerView.setAdapter(mAdapter);//设置adapter
        mRefreshLayout.setSuperRefreshLayoutListener(this);//设置下拉刷新和加载更多的监听
        mAdapter.setOnItemClickListener(this);//设置条目点击事件
        mRefreshLayout.setColorSchemeResources(
                R.color.swiperefresh_color1, R.color.swiperefresh_color2,
                R.color.swiperefresh_color3, R.color.swiperefresh_color4);

        mList = new ArrayList<>();

        if (isAutoLoad()) {
            mRefreshLayout.setRefreshing(true);//主动刷新，走下拉刷新逻辑
            onRefreshing();
        }
    }

    protected void setToolBar() {
        setToolBarTitle();
        setSupportActionBar(mToolbar);
        if (getNavigationIcon() > 0) {
            mToolbar.setNavigationIcon(getNavigationIcon());
        }
    }

    protected abstract void setToolBarTitle();

    protected int getNavigationIcon() {
        return R.mipmap.ic_back;
    }

    /**
     * 设置适配器
     */
    protected abstract BaseRecyclerAdapter<T> getRecyclerAdapter();

    /**
     * 设置布局管理器
     */
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getApplicationContext());
    }

    @Override
    public void setListener() {

    }

    @Override
    protected void parseData(ArrayList<T> data, String tag) {
        mList = data;
        nextPageIndex++;//请求成功后，修改下一页

        if (mIsRefresh) {//主动刷新
            mAdapter.clear();
            mAdapter.addAll(data);
            mRefreshLayout.setCanLoadMore(true);
            TLog.d("zyzd", getSimpleName() + ">>parseData--> " + "refresh");
        } else {
            mAdapter.addAll(data);
            TLog.d("zyzd", getSimpleName() + ">>parseData--> " + "loadMore");
        }
        if (data.size() < getPageSize()) {
            hasMore = false;
            mAdapter.setState(BaseRecyclerAdapter.STATE_NO_MORE, true);
        }
    }


    /**
     * @return 每页返回数据数量, 根据需求进行调整
     */
    protected int getPageSize() {
        return 10;
    }

    @Override
    public void onItemClick(int position, long itemId) {
        TLog.d("zyzd", "BaseRecyclerViewActivity>>onItemClick--> " + mAdapter.getItem(position).toString());
    }

    /**
     * 刷新
     */
    @Override
    public void onRefreshing() {
        TLog.d("zyzd", "BaseRecyclerViewActivity>>onRefreshing");
        mIsRefresh = true;//true 表示主动刷新
        stampTime = (new Date()).getTime();
        nextPageIndex = initPageIndex;
        hasMore = true;
        requestData();
    }

    /**
     * 请求数据
     */
    protected void requestData() {
        String url = getUrl(mIsRefresh);
        TLog.d("zyzd", "BaseRecyclerViewActivity>>requestData-url-> " + url);
        requestDataByGet(url);//直接用类名作为tag
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        if (hasMore) {
            mAdapter.setState(BaseRecyclerAdapter.STATE_LOADING, true);
            requestData();
        } else {//避免出现当没有更多数据时，导致下拉刷新失无效
            onComplete(getSimpleName());
        }
    }

    /**
     * 获取请求路径
     *
     * @param isRefresh
     * @return
     */
    protected abstract String getUrl(boolean isRefresh);


    /**
     * 请求完成（无论成功还是失败）
     */
    @Override
    protected void onRequestFinish(String tag) {
        onComplete(tag);
    }

    /**
     * 完成
     */
    protected void onComplete(String tag) {
        if (tag.equals(getSimpleName())) {
            mRefreshLayout.onComplete();
            mIsRefresh = false;
        }
    }

    /**
     * 是否首次自动加载
     *
     * @return
     */
    protected boolean isAutoLoad() {
        return true;
    }


}
